<template>
  <div class="user-info">

    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              style="width: 300px"
              type="date"
              class="inputW"
            >
              />
            </el-date-picker></el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input v-model="userInfo.departmentName" class="inputW" />
          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option
                v-for="item in context"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <upload ref="uploadImg" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveUser">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import context from '@/api/context'
import { userPhotoAPI, updateEmployeesInfoAPI } from '@/api'

export default {
  name: 'UserInfo',
  data() {
    return {
      userId: this.$route.query.id,
      context: context.hireType, // 员工枚举数据
      userInfo: {}
    }
  },
  created() {
    this.getUserInfoFn()
  },
  methods: {
    // 获取用户信息
    async getUserInfoFn() {
      const res = await userPhotoAPI(this.userId)
      //   console.log(res)
      this.userInfo = res.data
      this.$refs.uploadImg.imageUrl = res.data.staffPhoto

      //   如果后续给对象添加属性
      // $set()更新数组/对象某个值(并且额外添加数据劫持)
      // 参数1：数组/对象 目标
      // 参数2：下标/属性名
      // 参数3:值
      this.$set(this.userInfo, 'formOfEmployment', parseInt(this.$route.query.form))
    },

    //   保存用户信息
    async saveUser() {
      this.userInfo.staffPhoto = this.$refs.uploadImg.imageUrl
      const res = await updateEmployeesInfoAPI(this.userInfo)
      this.$message.success(res.message)
      this.$router.back()
    }
  }
}
</script>

  <style lang="scss" scoped></style>

